import '../utils/tools.dart';
import 'comparable.dart';

class SkuModel implements Equable<int> {
  int skuId = 0;
  String goodsNo = '';
  String image = '';
  double price = 0;
  double marketPrice = 0;
  double costPrice = 0;
  Map<int, double> extPrice = {};
  int storage = 0;
  int weight = 0;
  String size = '';

  int productId = 0;

  SkuModel.fromJson(Map sku) {
    skuId = Tools.parseInt(sku['sku_id']);
    goodsNo = sku['goods_no'] ?? '';
    price = Tools.parseNumber(sku['price'], lerant: 2);
    marketPrice = Tools.parseNumber(sku['market_price'], lerant: 2);
    costPrice = Tools.parseNumber(sku['cost_price'], lerant: 2);
    if (sku.containsKey('ext_price') && sku['ext_price'] is Map) {
      Map exts = sku['ext_price'];
      for (MapEntry entry in exts.entries) {
        extPrice[Tools.parseInt(entry.key)] =
            Tools.parseNumber(entry.value, lerant: 2);
      }
    }
    storage = Tools.parseInt(sku['storage']);
    weight = Tools.parseInt(sku['weight']);
    size = sku['size'] ?? '';

    image = Tools.fixImageUrl(sku['image']);

    productId = Tools.parseInt(sku['product_id']);
  }

  @override
  int get compareField => skuId;
}
